package com.mv.CodeStorage;

import java.util.LinkedList;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import beans.*;
import Export.CodeStorageContext.CodeStorageFactory;
import Interfaces.IDBItem;
import Models.Aplikace;
import Models.User;

@Controller
public class AppSupportController {
	
	private static final Logger logger = LoggerFactory.getLogger(HomeController.class);
	
	@RequestMapping(value = "/topapps", method = RequestMethod.GET)
	public String GetTopApps(HttpServletRequest request, Model model){
		
		logger.trace("top list");
		
		LinkedList<ApplicationBean> topApps = new LinkedList<ApplicationBean>();
		for(Aplikace item : CodeStorageFactory.LoadAplikaceTopHodList()){
			ApplicationBean bean = new ApplicationBean();
			bean.setId(item.getID());
			bean.setNazev(item.getNazev());
			bean.setHod(item.getPrumerHodnoceni());
			topApps.add(bean);
		}
		
		model.addAttribute("apps", topApps);
		
		return "noLayout:appLinkedList";
	}

	@RequestMapping(value = "/topdownapps", method = RequestMethod.GET)
	public String GetDownApps(HttpServletRequest request, Model model){
		
		logger.trace("down list");
		
		LinkedList<ApplicationBean> topApps = new LinkedList<ApplicationBean>();
		for(Aplikace item : CodeStorageFactory.LoadAplikaceTopStazList()){
			ApplicationBean bean = new ApplicationBean();
			bean.setId(item.getID());
			bean.setNazev(item.getNazev());
			bean.setPocetStazeni(item.getPocetStazeni());
			topApps.add(bean);
		}
		
		model.addAttribute("apps", topApps);
		
		return "noLayout:appLinkedList";
	}
	
	@RequestMapping(value = "/newapps", method = RequestMethod.GET)
	public String GetNewApps(HttpServletRequest request, Model model){
		
		logger.trace("new list");
		
		LinkedList<ApplicationBean> topApps = new LinkedList<ApplicationBean>();
		for(Aplikace item : CodeStorageFactory.LoadAplikaceNewList()){
			ApplicationBean bean = new ApplicationBean();
			bean.setId(item.getID());
			bean.setNazev(item.getNazev());
			bean.setDatum(item.getDatumVytvoreni().toString());
			topApps.add(bean);
		}
		
		model.addAttribute("apps", topApps);
		
		return "noLayout:appLinkedList";
	}
	
	@RequestMapping(value = "/search", method = RequestMethod.GET)
	public String Apps(HttpServletRequest request, Model model, 
			@RequestParam(value = "tag", required = false) String tag, 
			@RequestParam(value = "name", required = false) String nazev, 
			@RequestParam(value = "subscription", required = false) String popis,
			@RequestParam(value = "eval", required = false) Float eval){
		
		
		model.addAttribute("tag", tag);
		model.addAttribute("name", nazev);
		model.addAttribute("subscription", popis);
		model.addAttribute("eval", eval);

		LinkedList<ApplicationBean> topApps = new LinkedList<ApplicationBean>();
		for(Aplikace item : CodeStorageFactory.SearchAplikace(nazev, popis, tag, eval)){
			ApplicationBean bean = new ApplicationBean();
			bean.setId(item.getID());
			bean.setNazev(item.getNazev());
			bean.setPopis(item.getPopis());
			bean.setHod(item.getPrumerHodnoceni());
			topApps.add(bean);
		}
		
		model.addAttribute("title", "Code Storage - Vyhledávání aplikací");
		model.addAttribute("page", 1);
		model.addAttribute("apps", topApps);
		
		return "appTabulka";	
	}
	
	@RequestMapping(value = "/profile", method = RequestMethod.GET)
	public String AppUserProfile(HttpServletRequest request, Model model){
		User usr = CodeStorageFactory.LoadUserSimple(request.getUserPrincipal().getName());
		CodeStorageFactory.FetchUserAplikace(usr);
		
		Aplikace app;
		LinkedList<ApplicationBean> topApps = new LinkedList<ApplicationBean>();
		
		if(usr.getChilds() != null){
			for(IDBItem item : usr.getChilds()){
				app = (Aplikace)item;
				ApplicationBean bean = new ApplicationBean();
				bean.setId(app.getID());
				bean.setNazev(app.getNazev());
				bean.setPopis(app.getPopis());
				bean.setHod(app.getPrumerHodnoceni());
				topApps.add(bean);
			}		
		}
		
		UserBean usb = new UserBean();
		usb.setEmail(usr.GetEmail());
		usb.setLogin(usr.GetLogin());
		
		model.addAttribute("title", "Code Storage - "+usr.GetLogin());
		model.addAttribute("page", 3);
		model.addAttribute("usr", usb);
		model.addAttribute("apps", topApps);
		
		return "profile";
	}
	
	
	
}
